//
//  DISLectRightViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/6/18.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISLectRightViewController.h"
#import "DISgoodsDetailViewController.h"
#import "DISgoosRightViewController.h"
static int const labelWith = 100;

@interface DISLectRightViewController ()<UIScrollViewDelegate>{
    UILabel *mylab;
}
@property(nonatomic,weak)UIScrollView *titleScrollView;
@property(nonatomic,weak)UIScrollView *contentScrollView;
@property(nonatomic,strong)UIButton *oldButton;
@property(nonatomic,strong)UILabel *seletLabel;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,assign)BOOL isbutton;
@property(nonatomic,strong)UIView *buyAudBgview;
@property(nonatomic,strong)NSString *shopPhone;
@property(nonatomic,strong)NSDictionary *shopdic;

@end

@implementation DISLectRightViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

-(NSMutableArray *)titleArray{
    
    if (_titleArray == nil) {
        _titleArray = [NSMutableArray array];
    }
    return _titleArray;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isbutton = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, kNaviHeight-40, ScreenW, 40)];
    [self.view addSubview:_bgView];
    _bgView.userInteractionEnabled = YES;
    [self setScrollViewTopandBottom];
    [self setupViewControllers];
    _titleArray = [@[@"产品",@"详情"] mutableCopy];
    [self setTitlesButton];
    //[self reqestConpon];
    
    // Do any additional setup after loading the view.
    UIButton * _leftbutton = [[UIButton alloc] initWithFrame:CGRectMake(15, 0, 40, 40)];
    _leftbutton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_leftbutton setContentEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [_leftbutton setImage:imgname(@"left_icon") forState:UIControlStateNormal];
    [_leftbutton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [_leftbutton addTarget:self action:@selector(leftClickaction) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:_leftbutton];
    // Do any additional setup after loading the view.
}

- (void)leftClickaction{
    [super leftClick];
}

//初始化标题ScrollView
-(void)setupScrollView{
    NSInteger count = self.childViewControllers.count;
    //设置标题滚动条
    self.titleScrollView.contentSize = CGSizeMake(count * labelWith, 0);
    
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    //设置内容滚动条
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*count, 0);
    //开启分页
    self.contentScrollView.pagingEnabled = YES;
    //没有弹簧效果
    self.contentScrollView.bounces = NO;
    //隐藏水平滚动条
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //设置协议
    self.contentScrollView.delegate = self;
}

//设置页面scrollView
-(void)setScrollViewTopandBottom{
    
    UIScrollView *contentScroll = [[UIScrollView alloc]init];
    contentScroll.frame = CGRectMake(0, kNaviHeight, ScreenW, self.view.frame.size.height -40-50);
    contentScroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentScroll];
    self.contentScrollView = contentScroll;
    self.contentScrollView.userInteractionEnabled = YES;
    self.contentScrollView.scrollEnabled = NO;;
    self.contentScrollView.pagingEnabled = YES;
    //显示水平方向的滚动条(默认YES)
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    //显示垂直方向的滚动条(默认YES)
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.bounces = NO;
    self.contentScrollView.contentSize = CGSizeMake(ScreenW*3, ScreenH-37);
    self.contentScrollView.delegate = self;
}

#pragma mark -- UISCrollview代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    //  CGFloat offsetX = index * ScreenW;
    
    UIButton *but = (UIButton *)[self.view viewWithTag:100+index];
    [self.oldButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    [but setTitleColor:[UIColor colorWithHexString:@"#70BE68"] forState:UIControlStateNormal];
    self.oldButton = but;
    CGPoint CEN =  mylab.center;
    CEN.x = but.center.x;
    mylab.center = CEN;
    
    for (UIView *vie in self.view.subviews) {
        if (vie.frame.size.height  == KHEIGHT-kNaviHeight) {
            [vie removeFromSuperview];
        }
    }
}

-(void)setTitlesButton{
    
    [_bgView removeAllSubviews];
    NSArray *arr = _titleArray;
    CGFloat leftRight = KWIDTH/2-100;
    CGFloat wwww = (ScreenW-leftRight-leftRight)/arr.count;
    wwww =  labelWith;
    CGFloat fengxi = (ScreenW-leftRight-leftRight)/(arr.count-1);
    fengxi = 0;
    CGFloat hhhhh = 16;
    CGFloat yyyy =  +12 ;
    
    for (int i = 0; i<arr.count; i++) {
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton.frame = CGRectMake(leftRight+(fengxi+wwww)*i, yyyy, wwww, hhhhh);
        myButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_bgView addSubview:myButton];
        myButton.tag = 100+i;
        [myButton setTitle:arr[i] forState:UIControlStateNormal];
        [myButton addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [myButton setTitleColor:[self colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        if (i == 0) {
            [myButton setTitleColor:[self colorWithHexString:@"#333333"] forState:UIControlStateNormal];
            self.oldButton = myButton;
            [self titleClick:myButton];
            mylab = [[UILabel alloc]init];
            [_bgView addSubview:mylab];
            mylab.frame = CGRectMake(10, yyyy+hhhhh+4, 70, 2);
            CGPoint CEN = mylab.center;
            CEN.x = myButton.center.x;
            mylab.center = CEN;
            mylab.backgroundColor = [UIColor colorWithHexString:@"#70BE68"];
        }
    }
}

-(void)titleClick:(UIButton *)but{
    //0取出label
    NSInteger index = but.tag-100;
    //2.1计算滚动位置
    CGFloat offsetX = index * ScreenW;
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

//显示页面
-(void)showVC:(NSInteger)index{
    
    CGFloat offsetX = index * ScreenW;
    UIViewController *vc = self.childViewControllers[index];
    
    //判断当前控制器的View 有没有加载过 如果已经加载过 就不需要加载
    if (vc.isViewLoaded) return;
    vc.view.frame = CGRectMake(offsetX, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:vc.view];
}

- (void)setupViewControllers {

    DISgoodsDetailViewController *seveVC = [[DISgoodsDetailViewController alloc]init];
    seveVC.goodsID = self.goodsID;
    seveVC.view.frame = CGRectMake(0, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:seveVC.view];
    [self addChildViewController:seveVC];

    DISgoosRightViewController *evaluate = [[DISgoosRightViewController alloc]init];
    evaluate.goodsID = self.goodsID;
    evaluate.view.frame = CGRectMake(ScreenW, 0, ScreenW, self.contentScrollView.frame.size.height);
    [self.contentScrollView addSubview:evaluate.view];
    [self addChildViewController:evaluate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
